#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>
#define LEN 1024
#define MUM 32
int main()
{
  char cmd[LEN];
  char* myargv[MUM];
  while(1)
  {
    printf("[zxy@VM-24-17-centos lesson15]#");
    fgets(cmd,LEN,stdin);
    cmd[strlen(cmd)-1]='\0';//最后一个是换行符，改成'\0'
    //strtok分割字符串，返回字符串首字母地址
    int i=1;
    myargv[0]=strtok(cmd," ");
    while(myargv[i]=strtok(NULL," "))
    {
      i++;
    }
   //创建子进程
    pid_t id=fork();
    if(id==0)
    {
      //child
      execvp(myargv[0],myargv);
      exit(11);
    }
    else 
    {
      //father
      int status=0; 
      pid_t ret=waitpid(id,&status,0);
      if(ret>0)
      {
        printf("%d\n",WEXITSTATUS(status));
      }
      
    }
  }
  return 0;
}
